package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.SchoolXmEval
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.ui.RatingBarView
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.examine_class_objects_rank_add.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineClassObjectsRankActivity : AppEventBusTitleBaseActivity(), RatingBarView.OnRatingListener {

    var viewModel: ExamineViewModel? = null
    var classId: String? = null
    var className: String? = null

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<SchoolXmEval>) {
        if (result.eventType != "schoolTeacherEvalGetXmEval") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    showDataView(result.data)
                    setViewTitle("项目评估详情")
                } else {
                    showAddView()
                    setViewTitle("项目评估", "提交") {
                        submit()
                    }
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun showDataView(dto: SchoolXmEval) {
//        classNameTv.text = XUtils.string().getNotNullValue(dto.className)
//        // 头部
//        stEndTimeEt.setText(XUtils.string().getNotNullValue(dto.stEndTime))
//        unitNameEt.setText(XUtils.string().getNotNullValue(dto.unitName))
//        pxjgEt.setText(XUtils.string().getNotNullValue(dto.pxjg))
//
//        stEndTimeEt.isEnabled = false
//        unitNameEt.isEnabled = false
//        pxjgEt.isEnabled = false

        // 底部
        totalEt.text = XUtils.string().getNotNullValue(dto.total)
        textShEt.setText(XUtils.string().getNotNullValue(dto.textSh))
        textYjEt.setText(XUtils.string().getNotNullValue(dto.textYj))
        totalEt.isEnabled = false
        textShEt.isEnabled = false
        textYjEt.isEnabled = false

        // 分值处理
        value1.setStar(dto.score1.toInt(), true)
        value2.setStar(dto.score2.toInt(), true)
        value3.setStar(dto.score3.toInt(), true)
        value4.setStar(dto.score4.toInt(), true)
        value5.setStar(dto.score5.toInt(), true)

        value6.setStar(dto.score6.toInt(), true)
        value7.setStar(dto.score7.toInt(), true)
        value8.setStar(dto.score8.toInt(), true)
        value9.setStar(dto.score9.toInt(), true)
        value10.setStar(dto.score10.toInt(), true)

        value11.setStar(dto.score11.toInt(), true)
        value12.setStar(dto.score12.toInt(), true)
        value13.setStar(dto.score13.toInt(), true)
        value14.setStar(dto.score14.toInt(), true)
        value15.setStar(dto.score15.toInt(), true)

        value16.setStar(dto.score16.toInt(), true)
        value17.setStar(dto.score17.toInt(), true)
        value18.setStar(dto.score18.toInt(), true)
        value19.setStar(dto.score19.toInt(), true)
        value20.setStar(dto.score20.toInt(), true)

        value1.isClickable = false
        value2.isClickable = false
        value3.isClickable = false
        value4.isClickable = false
        value5.isClickable = false

        value6.isClickable = false
        value7.isClickable = false
        value8.isClickable = false
        value9.isClickable = false
        value10.isClickable = false

        value11.isClickable = false
        value12.isClickable = false
        value13.isClickable = false
        value14.isClickable = false
        value15.isClickable = false

        value16.isClickable = false
        value17.isClickable = false
        value18.isClickable = false
        value19.isClickable = false
        value20.isClickable = false
    }

    override fun getLayoutResId(): Int = R.layout.examine_class_objects_rank_add

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        classId = intent.getStringExtra("classId")
        className = intent.getStringExtra("className")

        viewModel = ExamineViewModel()

        showLoadingDialog("正在加载")
        viewModel!!.schoolTeacherEvalGetXmEval(classId!!)
    }

    private fun submit() {
        val schoolXmEval = SchoolXmEval()

        val user = LocalCache.getLoginUser()

        schoolXmEval.classId = classId
        schoolXmEval.evalUserId = user!!.id
        schoolXmEval.evalUserName = user.name
        schoolXmEval.className = className
//        // 头部
//        schoolXmEval.stEndTime = stEndTimeEt.text.toString()
//        schoolXmEval.unitName = unitNameEt.text.toString()
//        schoolXmEval.pxjg = pxjgEt.text.toString()
//        if (XUtils.string().isEmpty(schoolXmEval.stEndTime)) return XUtils.toast().show("请输入培训起止日期")
//        if (XUtils.string().isEmpty(schoolXmEval.unitName)) return XUtils.toast().show("请输入主办单位")
//        if (XUtils.string().isEmpty(schoolXmEval.pxjg)) return XUtils.toast().show("请输入培训机构")

        // 分值处理
        schoolXmEval.score1 = value1.starCount.toString()
        schoolXmEval.score2 = value2.starCount.toString()
        schoolXmEval.score3 = value3.starCount.toString()
        schoolXmEval.score4 = value4.starCount.toString()
        schoolXmEval.score5 = value5.starCount.toString()
        if (XUtils.string().isEmpty(schoolXmEval.score1)) return XUtils.toast().show("请给评估项1打分")
        if (XUtils.string().isEmpty(schoolXmEval.score2)) return XUtils.toast().show("请给评估项2打分")
        if (XUtils.string().isEmpty(schoolXmEval.score3)) return XUtils.toast().show("请给评估项3打分")
        if (XUtils.string().isEmpty(schoolXmEval.score4)) return XUtils.toast().show("请给评估项4打分")
        if (XUtils.string().isEmpty(schoolXmEval.score5)) return XUtils.toast().show("请给评估项5打分")

        schoolXmEval.score6 = value6.starCount.toString()
        schoolXmEval.score7 = value7.starCount.toString()
        schoolXmEval.score8 = value8.starCount.toString()
        schoolXmEval.score9 = value9.starCount.toString()
        schoolXmEval.score10 = value10.starCount.toString()
        if (XUtils.string().isEmpty(schoolXmEval.score6)) return XUtils.toast().show("请给评估项6打分")
        if (XUtils.string().isEmpty(schoolXmEval.score7)) return XUtils.toast().show("请给评估项7打分")
        if (XUtils.string().isEmpty(schoolXmEval.score8)) return XUtils.toast().show("请给评估项8打分")
        if (XUtils.string().isEmpty(schoolXmEval.score9)) return XUtils.toast().show("请给评估项9打分")
        if (XUtils.string().isEmpty(schoolXmEval.score10)) return XUtils.toast().show("请给评估项10打分")

        schoolXmEval.score11 = value11.starCount.toString()
        schoolXmEval.score12 = value12.starCount.toString()
        schoolXmEval.score13 = value13.starCount.toString()
        schoolXmEval.score14 = value14.starCount.toString()
        schoolXmEval.score15 = value15.starCount.toString()
        if (XUtils.string().isEmpty(schoolXmEval.score11)) return XUtils.toast().show("请给评估项11打分")
        if (XUtils.string().isEmpty(schoolXmEval.score12)) return XUtils.toast().show("请给评估项12打分")
        if (XUtils.string().isEmpty(schoolXmEval.score13)) return XUtils.toast().show("请给评估项13打分")
        if (XUtils.string().isEmpty(schoolXmEval.score14)) return XUtils.toast().show("请给评估项14打分")
        if (XUtils.string().isEmpty(schoolXmEval.score15)) return XUtils.toast().show("请给评估项15打分")

        schoolXmEval.score16 = value16.starCount.toString()
        schoolXmEval.score17 = value17.starCount.toString()
        schoolXmEval.score18 = value18.starCount.toString()
        schoolXmEval.score19 = value19.starCount.toString()
        schoolXmEval.score20 = value20.starCount.toString()
        if (XUtils.string().isEmpty(schoolXmEval.score16)) return XUtils.toast().show("请给评估项16打分")
        if (XUtils.string().isEmpty(schoolXmEval.score17)) return XUtils.toast().show("请给评估项17打分")
        if (XUtils.string().isEmpty(schoolXmEval.score18)) return XUtils.toast().show("请给评估项18打分")
        if (XUtils.string().isEmpty(schoolXmEval.score19)) return XUtils.toast().show("请给评估项19打分")
        if (XUtils.string().isEmpty(schoolXmEval.score20)) return XUtils.toast().show("请给评估项20打分")

        // 底部
        schoolXmEval.total = totalEt.text.toString()
        schoolXmEval.textSh = textShEt.text.toString()
        schoolXmEval.textYj = textYjEt.text.toString()
        if (XUtils.string().isEmpty(schoolXmEval.total)) return XUtils.toast().show("请输入总分")
        if (XUtils.string().isEmpty(schoolXmEval.textSh)) return XUtils.toast().show("请输入培训收获")
        if (XUtils.string().isEmpty(schoolXmEval.textYj)) return XUtils.toast().show("请输入改进描述")

        CommonDialog.Builder(this)
            .setCancelable(false)
            .setTitle("重要提示")
            .setMessage("评估后不可更改，确定提交吗?")
            .setPositiveButton("提交") {
                showLoadingDialog("正在提交")
                viewModel!!.schoolTeacherEvalSaveXmEval(schoolXmEval)
            }.setNegativeButton("取消") {
            }.show()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun addCallback(result: ResultInfo<SchoolXmEval>) {
        if (result.eventType != "schoolTeacherEvalSaveXmEval") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    XUtils.toast().showLong("评价已提交")
                    finish()
                } else {
                    XUtils.toast().show(result.msg)
                }
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun showAddView() {
        value1.setOnRatingListener(this)
        value2.setOnRatingListener(this)
        value3.setOnRatingListener(this)
        value4.setOnRatingListener(this)
        value5.setOnRatingListener(this)
        value6.setOnRatingListener(this)
        value7.setOnRatingListener(this)
        value8.setOnRatingListener(this)
        value9.setOnRatingListener(this)
        value10.setOnRatingListener(this)
        value11.setOnRatingListener(this)
        value12.setOnRatingListener(this)
        value13.setOnRatingListener(this)
        value14.setOnRatingListener(this)
        value15.setOnRatingListener(this)
        value16.setOnRatingListener(this)
        value17.setOnRatingListener(this)
        value18.setOnRatingListener(this)
        value19.setOnRatingListener(this)
        value20.setOnRatingListener(this)
    }

    override fun onRating(bindObject: Any?, RatingScore: Int) {
        totalEt.text = (
                value1.starCount +
                        value2.starCount +
                        value3.starCount +
                        value4.starCount +
                        value5.starCount +

                        value6.starCount +
                        value7.starCount +
                        value8.starCount +
                        value9.starCount +
                        value10.starCount +

                        value11.starCount +
                        value12.starCount +
                        value13.starCount +
                        value14.starCount +
                        value15.starCount +

                        value16.starCount +
                        value17.starCount +
                        value18.starCount +
                        value19.starCount +
                        value20.starCount
                ).toString()
    }
}